/*
 * Copyright (c) 2019.  黄钰朝
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hyc.www.dao;

import com.hyc.www.dao.annotation.Query;
import com.hyc.www.dao.annotation.Result;
import com.hyc.www.dao.annotation.ResultType;
import com.hyc.www.dao.core.BaseDao;
import com.hyc.www.dao.core.SQLRunner;
import com.hyc.www.dao.impl.SQLRunnerImpl;
import com.hyc.www.model.po.Ticket;
import com.hyc.www.model.po.User;

import java.math.BigInteger;
import java.sql.Connection;
import java.util.List;

/**
 * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
 * @description 负责Ticket类CRUD
 * @date 2019-07-17
 */
public interface TicketDao extends BaseDao {
    String TABLE = "ticket";
    String ALL_FIELD = "number,origin,destination,depart,arrive,price,inventory," + BASE_FIELD;

    SQLRunner runner = new SQLRunnerImpl();

    /**
     * 查询车票
     *
     * @param selectField 要查询的字段
     * @param ticket      where中的限制条件
     * @name listTicket
     * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
     * @date 2019/7/17
     */
    @Result(entity = Ticket.class, returns = ResultType.LIST)
    @Query()
    List listTicket(String[] selectField, Object ticket);



    /**
     * 通过id查询车票
     *
     * @param id
     * @return 车票对象
     * @name getTicketById
     * @notice none
     * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
     * @date 2019/7/18
     */
    @Result(entity = Ticket.class, returns = ResultType.OBJECT)
    @Query(value = "select " + ALL_FIELD + " from " + TABLE + " where id = ? ")
    Ticket getTicketById(BigInteger id);


    static Ticket getTicketById(Connection connection, Object id) {
        String sql = "select " + ALL_FIELD + " from " + TABLE + " where id = ? ";
        Object[] params = {id};
        return (Ticket) runner.queryObject(sql, params, Ticket.class, connection);
    }


    static int update(Connection connection, Ticket ticket) {
        return runner.update(ticket, TABLE, connection);

    }



}
